import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useKarmaStore = defineStore('karma', () => {
  // 状态
  const karmaRecords = ref([])
  const loading = ref(false)

  // 业力规则配置 - 扩展更多类型
  const karmaRules = ref({
    goodDeeds: {
      // 慈善救助类
      charity_donation: { score: 20, name: '慈善捐赠', category: '慈善救助', level: '中等' },
      help_poor: { score: 15, name: '扶贫济困', category: '慈善救助', level: '中等' },
      disaster_relief: { score: 25, name: '赈灾救助', category: '慈善救助', level: '重大' },

      // 救助生命类
      save_life: { score: 100, name: '救人性命', category: '救助生命', level: '重大' },
      rescue_animal: { score: 30, name: '救助动物', category: '救助生命', level: '中等' },
      medical_help: { score: 40, name: '医疗救助', category: '救助生命', level: '重大' },

      // 社会服务类
      volunteer: { score: 15, name: '志愿服务', category: '社会服务', level: '轻微' },
      teach_others: { score: 20, name: '传授知识', category: '社会服务', level: '中等' },
      community_service: { score: 12, name: '社区服务', category: '社会服务', level: '轻微' },

      // 孝道仁义类
      filial_piety: { score: 25, name: '孝敬父母', category: '孝道仁义', level: '重大' },
      care_elderly: { score: 18, name: '照顾老人', category: '孝道仁义', level: '中等' },
      help_children: { score: 15, name: '关爱儿童', category: '孝道仁义', level: '中等' },

      // 环境保护类
      plant_trees: { score: 10, name: '植树造林', category: '环境保护', level: '轻微' },
      protect_environment: { score: 12, name: '环境保护', category: '环境保护', level: '轻微' },
      wildlife_protection: { score: 20, name: '保护野生动物', category: '环境保护', level: '中等' }
    },
    badDeeds: {
      // 犯罪类
      murder: { score: -200, name: '杀害生灵', category: '重大犯罪', level: '极重' },
      violence: { score: -50, name: '暴力伤害', category: '重大犯罪', level: '重大' },
      rape: { score: -150, name: '强奸罪', category: '重大犯罪', level: '极重' },
      kidnap: { score: -120, name: '绑架勒索', category: '重大犯罪', level: '极重' },

      // 财产犯罪类
      theft: { score: -30, name: '偷盗行为', category: '财产犯罪', level: '中等' },
      robbery: { score: -80, name: '抢劫财物', category: '财产犯罪', level: '重大' },
      fraud: { score: -40, name: '欺诈行为', category: '财产犯罪', level: '中等' },
      embezzlement: { score: -60, name: '贪污挪用', category: '财产犯罪', level: '重大' },

      // 道德败坏类
      adultery: { score: -25, name: '通奸背叛', category: '道德败坏', level: '中等' },
      gambling: { score: -20, name: '赌博成瘾', category: '道德败坏', level: '轻微' },
      alcoholism: { score: -15, name: '酗酒滋事', category: '道德败坏', level: '轻微' },
      drug_abuse: { score: -35, name: '吸毒贩毒', category: '道德败坏', level: '重大' },

      // 欺凌虐待类
      bullying: { score: -18, name: '霸凌他人', category: '欺凌虐待', level: '轻微' },
      animal_cruelty: { score: -30, name: '虐待动物', category: '欺凌虐待', level: '中等' },
      child_abuse: { score: -80, name: '虐待儿童', category: '欺凌虐待', level: '重大' },
      elder_abuse: { score: -70, name: '虐待老人', category: '欺凌虐待', level: '重大' },

      // 背叛忘恩类
      betrayal: { score: -35, name: '背叛友情', category: '背叛忘恩', level: '中等' },
      ungrateful: { score: -40, name: '忘恩负义', category: '背叛忘恩', level: '中等' },
      unfilial: { score: -50, name: '不孝父母', category: '背叛忘恩', level: '重大' }
    }
  })

  // 业力等级分类
  const karmaLevels = ref([
    { range: [300, Infinity], level: '圣贤', color: '#52c41a', description: '德高望重，功德圆满' },
    { range: [200, 299], level: '大善', color: '#73d13d', description: '积德深厚，善缘广结' },
    { range: [100, 199], level: '善人', color: '#95de64', description: '心地善良，乐善好施' },
    { range: [50, 99], level: '良民', color: '#b7eb8f', description: '品行端正，遵纪守法' },
    { range: [0, 49], level: '平民', color: '#faad14', description: '善恶参半，平平无奇' },
    { range: [-49, -1], level: '小恶', color: '#ffc069', description: '偶有过失，尚可救赎' },
    { range: [-99, -50], level: '恶人', color: '#ff7a45', description: '作恶多端，罪孽深重' },
    { range: [-199, -100], level: '大恶', color: '#ff4d4f', description: '十恶不赦，天理难容' },
    { range: [-Infinity, -200], level: '魔鬼', color: '#820014', description: '罪恶滔天，万劫不复' }
  ])

  // 报应时机预测规则
  const retributionRules = ref({
    immediate: { threshold: 80, probability: 0.9, timeRange: '1-7天' },
    shortTerm: { threshold: 40, probability: 0.7, timeRange: '1-6个月' },
    mediumTerm: { threshold: 20, probability: 0.5, timeRange: '1-3年' },
    longTerm: { threshold: 10, probability: 0.3, timeRange: '来世' }
  })

  // 模拟详细假数据
  const mockData = ref([
    {
      id: 1,
      personId: 1001,
      personName: '张三',
      type: 'goodDeed',
      action: 'save_life',
      score: 100,
      description: '在河边救起落水儿童，并将其安全送回家中',
      date: '2024-12-01',
      time: '14:30',
      location: '北京市朝阳区朝阳公园',
      witnesses: ['李四', '王五', '公园保安'],
      beneficiaries: ['落水儿童-小明(8岁)', '小明家人'],
      impact: '挽救一条生命，获得家属和社会赞誉',
      evidence: ['现场视频', '新闻报道', '锦旗证明'],
      karmaCategory: '救助生命',
      verified: true,
      verifiedBy: '朝阳区派出所',
      createdAt: '2024-12-01 15:00:00'
    },
    {
      id: 2,
      personId: 1002,
      personName: '李四',
      type: 'badDeed',
      action: 'theft',
      score: -30,
      description: '在商场内偷取他人钱包，价值约500元',
      date: '2024-11-28',
      time: '16:45',
      location: '上海市浦东新区世纪大道购物中心',
      witnesses: ['张三', '商场保安'],
      victims: ['钱包失主-王女士'],
      consequences: '被商场保安抓获，现金和证件被盗',
      evidence: ['监控录像', '报警记录', '失主报案'],
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '浦东新区派出所',
      createdAt: '2024-11-28 17:00:00'
    },
    {
      id: 3,
      personId: 1001,
      personName: '张三',
      type: 'goodDeed',
      action: 'charity_donation',
      score: 20,
      description: '向山区希望小学捐赠书籍和学习用品',
      date: '2024-11-25',
      time: '09:00',
      location: '陕西省延安市某希望小学',
      witnesses: ['学校校长', '当地志愿者'],
      beneficiaries: ['山区儿童50名', '希望小学'],
      impact: '改善50名儿童学习条件，提供新书籍200本',
      evidence: ['捐赠证书', '受赠学校感谢信'],
      karmaCategory: '慈善救助',
      verified: true,
      verifiedBy: '当地教育部门',
      createdAt: '2024-11-25 10:00:00'
    },
    {
      id: 4,
      personId: 1003,
      personName: '王五',
      type: 'badDeed',
      action: 'violence',
      score: -50,
      description: '酒后与他人发生冲突，造成对方轻伤',
      date: '2024-11-20',
      time: '22:30',
      location: '广州市天河区某酒吧',
      witnesses: ['酒吧服务员', '其他顾客'],
      victims: ['受伤者-赵某'],
      consequences: '造成他人鼻梁骨折，医疗费用3000元',
      evidence: ['医院诊断书', '监控录像', '110报警记录'],
      karmaCategory: '重大犯罪',
      verified: true,
      verifiedBy: '天河区派出所',
      createdAt: '2024-11-20 23:00:00'
    },
    {
      id: 5,
      personId: 1004,
      personName: '赵六',
      type: 'goodDeed',
      action: 'filial_piety',
      score: 25,
      description: '十年如一日照顾瘫痪在床的母亲',
      date: '2024-11-15',
      time: '全天',
      location: '成都市武侯区家中',
      witnesses: ['邻居们', '社区工作人员'],
      beneficiaries: ['母亲-赵老太太(78岁)'],
      impact: '用爱心和耐心照顾母亲，获得社区表彰',
      evidence: ['社区表彰书', '邻居证明'],
      karmaCategory: '孝道仁义',
      verified: true,
      verifiedBy: '社区居委会',
      createdAt: '2024-11-15 18:00:00'
    },
    {
      id: 6,
      personId: 1005,
      personName: '孙七',
      type: 'badDeed',
      action: 'fraud',
      score: -40,
      description: '通过网络虚假投资项目诈骗他人资金',
      date: '2024-10-10',
      time: '14:00-18:00',
      location: '深圳市南山区某写字楼',
      witnesses: ['同事', '投资受害者'],
      victims: ['多名投资者，累计被骗金额15万'],
      consequences: '多人财产损失，影响家庭生活',
      evidence: ['转账记录', '虚假宣传材料', '报案记录'],
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '南山区经侦大队',
      createdAt: '2024-10-10 19:00:00'
    },
    {
      id: 7,
      personId: 1006,
      personName: '钱八',
      type: 'goodDeed',
      action: 'volunteer',
      score: 15,
      description: '参与社区志愿服务，协助老人出行',
      date: '2024-10-05',
      time: '08:00',
      location: '杭州市西湖区社区服务中心',
      witnesses: ['社区工作人员', '受助老人'],
      beneficiaries: ['社区老人10余名'],
      impact: '帮助老人解决出行难题，获得社区好评',
      evidence: ['志愿服务证书', '社区表彰'],
      karmaCategory: '社会服务',
      verified: true,
      verifiedBy: '社区居委会',
      createdAt: '2024-10-05 09:00:00'
    },
    {
      id: 8,
      personId: 1007,
      personName: '周九',
      type: 'goodDeed',
      action: 'help_poor',
      score: 15,
      description: '为贫困家庭提供生活用品和食物',
      date: '2024-09-20',
      time: '10:00',
      location: '重庆市渝中区某贫困社区',
      witnesses: ['慈善机构工作人员'],
      beneficiaries: ['贫困家庭5户'],
      impact: '改善贫困家庭生活条件，送去温暖',
      evidence: ['捐赠清单', '受益家庭感谢信'],
      karmaCategory: '慈善救助',
      verified: true,
      verifiedBy: '慈善机构',
      createdAt: '2024-09-20 11:00:00'
    },
    {
      id: 9,
      personId: 1008,
      personName: '吴十',
      type: 'goodDeed',
      action: 'plant_trees',
      score: 10,
      description: '参与植树节活动，种植树苗50棵',
      date: '2024-09-15',
      time: '08:00',
      location: '武汉市东湖新技术开发区植树基地',
      witnesses: ['环保组织工作人员', '其他志愿者'],
      beneficiaries: ['环境保护', '后代子孙'],
      impact: '改善生态环境，为城市增添绿色',
      evidence: ['植树活动照片', '环保组织证明'],
      karmaCategory: '环境保护',
      verified: true,
      verifiedBy: '环保局',
      createdAt: '2024-09-15 17:00:00'
    },
    // 添加更多丰富的善行记录
    {
      id: 10,
      personId: 1009,
      personName: '郑十一',
      type: 'goodDeed',
      action: 'medical_help',
      score: 40,
      description: '义务为山区居民提供医疗服务，免费治疗患者30余名',
      date: '2024-09-10',
      time: '全天',
      location: '云南省大理州偏远山村',
      witnesses: ['村委会主任', '村民代表'],
      beneficiaries: ['山区居民30余名', '偏远山村'],
      impact: '解决山区就医难问题，挽救多名重病患者',
      evidence: ['义诊记录', '村委会感谢信', '患者康复证明'],
      karmaCategory: '救助生命',
      verified: true,
      verifiedBy: '大理州卫健委',
      createdAt: '2024-09-10 20:00:00'
    },
    {
      id: 11,
      personId: 1010,
      personName: '冯十二',
      type: 'goodDeed',
      action: 'teach_others',
      score: 20,
      description: '义务为留守儿童辅导功课，连续支教三个月',
      date: '2024-08-30',
      time: '19:00-21:00',
      location: '湖南省永州市某小学',
      witnesses: ['学校老师', '学生家长'],
      beneficiaries: ['留守儿童25名'],
      impact: '提高留守儿童学习成绩，关爱心理健康',
      evidence: ['支教证明', '学生成绩提升报告'],
      karmaCategory: '社会服务',
      verified: true,
      verifiedBy: '永州市教育局',
      createdAt: '2024-08-30 22:00:00'
    },
    {
      id: 12,
      personId: 1011,
      personName: '陈十三',
      type: 'goodDeed',
      action: 'rescue_animal',
      score: 30,
      description: '救助受伤的流浪猫狗，并为其提供医疗和收养服务',
      date: '2024-08-25',
      time: '下午',
      location: '西安市雁塔区动物救助站',
      witnesses: ['动物救助站工作人员'],
      beneficiaries: ['流浪动物15只'],
      impact: '拯救流浪动物生命，促进动物保护意识',
      evidence: ['救助记录', '医疗费用单据', '收养证明'],
      karmaCategory: '救助生命',
      verified: true,
      verifiedBy: '动物保护协会',
      createdAt: '2024-08-25 18:00:00'
    },
    {
      id: 13,
      personId: 1012,
      personName: '卫十四',
      type: 'goodDeed',
      action: 'disaster_relief',
      score: 25,
      description: '台风灾害后主动参与救灾工作，连续奋战72小时',
      date: '2024-08-15',
      time: '全天候',
      location: '福建省福州市受灾地区',
      witnesses: ['救灾指挥部', '受灾群众'],
      beneficiaries: ['受灾群众200余户'],
      impact: '协助转移受困群众，运送救援物资',
      evidence: ['救灾工作证明', '指挥部表彰书'],
      karmaCategory: '慈善救助',
      verified: true,
      verifiedBy: '福州市应急管理局',
      createdAt: '2024-08-15 23:59:00'
    },
    {
      id: 14,
      personId: 1013,
      personName: '蒋十五',
      type: 'goodDeed',
      action: 'care_elderly',
      score: 18,
      description: '每周定期探望独居老人，提供生活照料',
      date: '2024-08-01',
      time: '每周六日',
      location: '南京市玄武区养老院',
      witnesses: ['养老院护工', '其他志愿者'],
      beneficiaries: ['独居老人8名'],
      impact: '让老人感受到社会关爱，改善心理状态',
      evidence: ['志愿服务记录', '老人感谢信'],
      karmaCategory: '孝道仁义',
      verified: true,
      verifiedBy: '玄武区民政局',
      createdAt: '2024-08-01 17:00:00'
    },
    {
      id: 15,
      personId: 1014,
      personName: '韩十六',
      type: 'goodDeed',
      action: 'help_children',
      score: 15,
      description: '为困难家庭儿童免费提供课外培训',
      date: '2024-07-20',
      time: '周末',
      location: '天津市和平区社区活动中心',
      witnesses: ['社区工作人员', '学生家长'],
      beneficiaries: ['困难家庭儿童12名'],
      impact: '提升困难家庭儿童综合素质，减轻家庭负担',
      evidence: ['免费培训证明', '家长感谢信'],
      karmaCategory: '孝道仁义',
      verified: true,
      verifiedBy: '和平区教育局',
      createdAt: '2024-07-20 19:00:00'
    },
    {
      id: 16,
      personId: 1015,
      personName: '杨十七',
      type: 'goodDeed',
      action: 'community_service',
      score: 12,
      description: '组织社区清洁活动，美化居住环境',
      date: '2024-07-10',
      time: '07:00-12:00',
      location: '济南市历下区某小区',
      witnesses: ['物业管理人员', '小区居民'],
      beneficiaries: ['小区全体居民'],
      impact: '改善小区环境卫生，提升居住品质',
      evidence: ['活动照片', '物业表扬信'],
      karmaCategory: '社会服务',
      verified: true,
      verifiedBy: '历下区物业办',
      createdAt: '2024-07-10 13:00:00'
    },
    {
      id: 17,
      personId: 1016,
      personName: '朱十八',
      type: 'goodDeed',
      action: 'protect_environment',
      score: 12,
      description: '举报并协助处理非法排污行为',
      date: '2024-06-25',
      time: '发现时立即行动',
      location: '苏州市工业园区某工厂',
      witnesses: ['环保执法人员'],
      beneficiaries: ['周边居民', '环境保护'],
      impact: '阻止环境污染，保护周边水源安全',
      evidence: ['举报记录', '环保部门处理结果'],
      karmaCategory: '环境保护',
      verified: true,
      verifiedBy: '苏州市环保局',
      createdAt: '2024-06-25 16:00:00'
    },
    {
      id: 18,
      personId: 1017,
      personName: '秦十九',
      type: 'goodDeed',
      action: 'wildlife_protection',
      score: 20,
      description: '参与野生动物保护行动，救助受伤东北虎',
      date: '2024-06-15',
      time: '紧急出动',
      location: '黑龙江省哈尔滨市郊外',
      witnesses: ['野生动物保护专家', '林业局工作人员'],
      beneficiaries: ['受伤东北虎', '野生动物保护事业'],
      impact: '成功救助濒危野生动物，促进生态平衡',
      evidence: ['救助记录', '专家鉴定报告', '新闻报道'],
      karmaCategory: '环境保护',
      verified: true,
      verifiedBy: '黑龙江省林业局',
      createdAt: '2024-06-15 20:00:00'
    },
    {
      id: 19,
      personId: 1001,
      personName: '张三',
      type: 'goodDeed',
      action: 'volunteer',
      score: 15,
      description: '疫情期间主动参与社区防控志愿服务',
      date: '2024-06-01',
      time: '全天',
      location: '北京市朝阳区各社区卡点',
      witnesses: ['社区工作人员', '居民代表'],
      beneficiaries: ['社区全体居民'],
      impact: '协助社区做好疫情防控，保障居民健康',
      evidence: ['志愿服务证书', '社区表彰'],
      karmaCategory: '社会服务',
      verified: true,
      verifiedBy: '朝阳区疫情防控指挥部',
      createdAt: '2024-06-01 22:00:00'
    },
    {
      id: 20,
      personId: 1018,
      personName: '尤二十',
      type: 'goodDeed',
      action: 'save_life',
      score: 100,
      description: '火灾现场勇救被困老人，不顾个人安危',
      date: '2024-05-20',
      time: '凌晨2:00',
      location: '广州市番禺区某居民楼',
      witnesses: ['消防队员', '周边居民'],
      beneficiaries: ['被困老人-李大爷(82岁)', '老人家属'],
      impact: '在火灾中挽救生命，展现无私奉献精神',
      evidence: ['消防部门证明', '新闻报道', '家属锦旗'],
      karmaCategory: '救助生命',
      verified: true,
      verifiedBy: '番禺区消防大队',
      createdAt: '2024-05-20 08:00:00'
    },
    // 增加更多恶行记录
    {
      id: 21,
      personId: 2001,
      personName: '马强',
      type: 'badDeed',
      action: 'robbery',
      score: -80,
      description: '持刀抢劫路人财物，造成被害人轻伤',
      date: '2024-12-05',
      time: '23:30',
      location: '上海市静安区某小巷',
      witnesses: ['路过市民', '110报警群众'],
      victims: ['被抢劫市民-刘某'],
      consequences: '抢走现金1200元和手机一部，被害人受轻伤',
      evidence: ['监控录像', '110报警记录', '医院诊断书', '赃物清单'],
      status: 'processed',
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '静安区派出所',
      createdAt: '2024-12-05 23:45:00'
    },
    {
      id: 22,
      personId: 2002,
      personName: '田霸',
      type: 'badDeed',
      action: 'violence',
      score: -50,
      description: '因停车纠纷殴打他人，造成多处软组织挫伤',
      date: '2024-12-03',
      time: '18:20',
      location: '北京市海淀区某小区停车场',
      witnesses: ['小区保安', '围观居民'],
      victims: ['车主-王某'],
      consequences: '造成被害人脸部肿胀、身体多处淤青',
      evidence: ['现场监控', '医疗诊断', '证人证言'],
      status: 'investigating',
      karmaCategory: '重大犯罪',
      verified: true,
      verifiedBy: '海淀区派出所',
      createdAt: '2024-12-03 19:00:00'
    },
    {
      id: 23,
      personId: 2003,
      personName: '何贪',
      type: 'badDeed',
      action: 'embezzlement',
      score: -60,
      description: '利用职务便利贪污公款，数额较大',
      date: '2024-11-30',
      time: '工作期间',
      location: '深圳市福田区某政府部门',
      witnesses: ['审计人员', '同事'],
      victims: ['国家财产', '公共利益'],
      consequences: '贪污挪用公款8万余元，损害公共利益',
      evidence: ['审计报告', '银行流水', '财务凭证'],
      status: 'processed',
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '福田区纪委监委',
      createdAt: '2024-11-30 16:00:00'
    },
    {
      id: 24,
      personId: 2004,
      personName: '龙虐',
      type: 'badDeed',
      action: 'child_abuse',
      score: -80,
      description: '长期虐待继子，造成身心严重伤害',
      date: '2024-11-25',
      time: '长期',
      location: '广州市天河区家中',
      witnesses: ['邻居', '学校老师'],
      victims: ['继子-小明(10岁)'],
      consequences: '造成儿童身体多处伤痕，心理创伤严重',
      evidence: ['医院诊断', '学校报告', '邻居证言', '照片证据'],
      status: 'processed',
      karmaCategory: '欺凌虐待',
      verified: true,
      verifiedBy: '天河区公安分局',
      createdAt: '2024-11-25 14:00:00'
    },
    {
      id: 25,
      personId: 2005,
      personName: '牛狠',
      type: 'badDeed',
      action: 'animal_cruelty',
      score: -30,
      description: '故意虐待流浪猫，手段残忍',
      date: '2024-11-20',
      time: '下午',
      location: '杭州市西湖区某小区',
      witnesses: ['小区居民', '动物保护志愿者'],
      victims: ['流浪猫3只'],
      consequences: '造成动物死亡和伤残，影响恶劣',
      evidence: ['现场照片', '视频录像', '动保组织报告'],
      status: 'pending',
      karmaCategory: '欺凌虐待',
      verified: true,
      verifiedBy: '动物保护协会',
      createdAt: '2024-11-20 17:00:00'
    },
    {
      id: 26,
      personId: 2006,
      personName: '羊骗',
      type: 'badDeed',
      action: 'fraud',
      score: -40,
      description: '冒充公检法人员电信诈骗，骗取老人养老金',
      date: '2024-11-15',
      time: '10:00-16:00',
      location: '网络/电话',
      witnesses: ['银行工作人员', '报案人家属'],
      victims: ['退休老人-张大妈(68岁)'],
      consequences: '骗取老人毕生积蓄5万元，严重影响生活',
      evidence: ['通话录音', '转账记录', '银行监控'],
      status: 'investigating',
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '网络安全大队',
      createdAt: '2024-11-15 18:00:00'
    },
    {
      id: 27,
      personId: 2007,
      personName: '朱醉',
      type: 'badDeed',
      action: 'alcoholism',
      score: -15,
      description: '酗酒后在公共场所滋事，妨害公共秩序',
      date: '2024-11-10',
      time: '22:00',
      location: '成都市锦江区春熙路',
      witnesses: ['路人', '商家', '执勤民警'],
      victims: ['过路行人', '商家'],
      consequences: '影响公共秩序，造成交通阻塞',
      evidence: ['现场视频', '民警执法记录'],
      status: 'processed',
      karmaCategory: '道德败坏',
      verified: true,
      verifiedBy: '锦江区派出所',
      createdAt: '2024-11-10 23:00:00'
    },
    {
      id: 28,
      personId: 2008,
      personName: '侯赌',
      type: 'badDeed',
      action: 'gambling',
      score: -20,
      description: '参与网络赌博，输掉家庭全部积蓄',
      date: '2024-11-05',
      time: '长期',
      location: '网络赌博平台',
      witnesses: ['家属', '银行工作人员'],
      victims: ['家庭成员', '自己'],
      consequences: '败光家产15万，家庭破裂',
      evidence: ['银行流水', '赌博网站记录', '家属报案'],
      status: 'pending',
      karmaCategory: '道德败坏',
      verified: true,
      verifiedBy: '经侦大队',
      createdAt: '2024-11-05 20:00:00'
    },
    {
      id: 29,
      personId: 2009,
      personName: '邵毒',
      type: 'badDeed',
      action: 'drug_abuse',
      score: -35,
      description: '吸食并贩卖毒品，毒害青少年',
      date: '2024-10-30',
      time: '夜间',
      location: '武汉市汉口区某娱乐场所',
      witnesses: ['禁毒民警', '现场人员'],
      victims: ['青少年群体', '社会'],
      consequences: '传播毒品危害，影响青少年身心健康',
      evidence: ['毒品检验报告', '现场缴获物品', '供述记录'],
      status: 'processed',
      karmaCategory: '道德败坏',
      verified: true,
      verifiedBy: '禁毒支队',
      createdAt: '2024-10-30 02:00:00'
    },
    {
      id: 30,
      personId: 2010,
      personName: '崔背',
      type: 'badDeed',
      action: 'betrayal',
      score: -35,
      description: '出卖多年好友商业机密，导致朋友公司破产',
      date: '2024-10-25',
      time: '商业谈判期间',
      location: '重庆市渝中区某商务楼',
      witnesses: ['竞争对手', '律师'],
      victims: ['好友及其公司', '公司员工'],
      consequences: '导致朋友公司倒闭，30余人失业',
      evidence: ['商业合同', '邮件记录', '证人证言'],
      status: 'investigating',
      karmaCategory: '背叛忘恩',
      verified: true,
      verifiedBy: '商事仲裁委',
      createdAt: '2024-10-25 15:00:00'
    },
    {
      id: 31,
      personId: 2011,
      personName: '魏孝',
      type: 'badDeed',
      action: 'unfilial',
      score: -50,
      description: '将年迈父母赶出家门，拒绝赡养',
      date: '2024-10-20',
      time: '全天',
      location: '西安市雁塔区家中',
      witnesses: ['邻居', '社区工作人员'],
      victims: ['年迈父母'],
      consequences: '老人无家可归，身体健康每况愈下',
      evidence: ['邻居证言', '社区调解记录', '医院诊断'],
      status: 'pending',
      karmaCategory: '背叛忘恩',
      verified: true,
      verifiedBy: '雁塔区法院',
      createdAt: '2024-10-20 18:00:00'
    },
    {
      id: 32,
      personId: 2012,
      personName: '汤恶',
      type: 'badDeed',
      action: 'bullying',
      score: -18,
      description: '在学校长期霸凌同学，造成心理创伤',
      date: '2024-10-15',
      time: '学校时间',
      location: '南京市鼓楼区某中学',
      witnesses: ['同学', '老师'],
      victims: ['被霸凌同学-小李'],
      consequences: '造成被害人不敢上学，产生严重心理阴影',
      evidence: ['学校调查报告', '心理医生诊断', '同学证言'],
      status: 'processed',
      karmaCategory: '欺凌虐待',
      verified: true,
      verifiedBy: '学校纪律委员会',
      createdAt: '2024-10-15 16:00:00'
    },
    {
      id: 33,
      personId: 2013,
      personName: '姚忘',
      type: 'badDeed',
      action: 'ungrateful',
      score: -40,
      description: '受恩师多年栽培，成功后反咬一口陷害恩师',
      date: '2024-10-10',
      time: '公开场合',
      location: '苏州市姑苏区某公司',
      witnesses: ['公司同事', '媒体记者'],
      victims: ['恩师-李教授'],
      consequences: '导致恩师名誉受损，被迫提前退休',
      evidence: ['媒体报道', '录音记录', '证人证言'],
      status: 'investigating',
      karmaCategory: '背叛忘恩',
      verified: true,
      verifiedBy: '教育纪检部门',
      createdAt: '2024-10-10 20:00:00'
    },
    {
      id: 34,
      personId: 2014,
      personName: '唐盗',
      type: 'badDeed',
      action: 'theft',
      score: -30,
      description: '多次入室盗窃，专挑独居老人下手',
      date: '2024-10-05',
      time: '深夜',
      location: '天津市和平区多个小区',
      witnesses: ['小区保安', '邻居'],
      victims: ['多名独居老人'],
      consequences: '盗窃财物价值2万余元，老人心理受创',
      evidence: ['监控录像', '指纹鉴定', '赃物清单'],
      status: 'processed',
      karmaCategory: '财产犯罪',
      verified: true,
      verifiedBy: '和平区刑警队',
      createdAt: '2024-10-05 08:00:00'
    },
    {
      id: 35,
      personId: 2015,
      personName: '费暴',
      type: 'badDeed',
      action: 'elder_abuse',
      score: -70,
      description: '长期虐待患病老母，不给医治',
      date: '2024-09-30',
      time: '长期',
      location: '济南市历城区家中',
      witnesses: ['社区工作人员', '医护人员'],
      victims: ['患病老母-费老太(75岁)'],
      consequences: '老人病情恶化，身心饱受折磨',
      evidence: ['医院诊断', '社区走访记录', '邻居举报'],
      status: 'pending',
      karmaCategory: '欺凌虐待',
      verified: true,
      verifiedBy: '历城区民政局',
      createdAt: '2024-09-30 14:00:00'
    }
  ])

  // 计算属性
  const totalKarmaCount = computed(() => karmaRecords.value.length)
  const goodDeedsCount = computed(() => karmaRecords.value.filter((r) => r.score > 0).length)
  const badDeedsCount = computed(() => karmaRecords.value.filter((r) => r.score < 0).length)

  // 按人员统计业力
  const karmaByPerson = computed(() => {
    const result = {}
    karmaRecords.value.forEach((record) => {
      if (!result[record.personId]) {
        result[record.personId] = {
          personId: record.personId,
          personName: record.personName,
          totalScore: 0,
          goodDeeds: 0,
          badDeeds: 0,
          records: []
        }
      }
      result[record.personId].totalScore += record.score
      if (record.score > 0) {
        result[record.personId].goodDeeds += record.score
      } else {
        result[record.personId].badDeeds += Math.abs(record.score)
      }
      result[record.personId].records.push(record)
    })
    return Object.values(result).sort((a, b) => b.totalScore - a.totalScore)
  })

  // 业力趋势分析
  const karmaStatistics = computed(() => {
    const stats = {
      totalRecords: karmaRecords.value.length,
      goodDeedsStats: {},
      badDeedsStats: {},
      monthlyTrend: {},
      categoryDistribution: {},
      levelDistribution: {}
    }

    // 善行统计
    const goodDeeds = karmaRecords.value.filter((r) => r.score > 0)
    stats.goodDeedsStats = {
      count: goodDeeds.length,
      totalScore: goodDeeds.reduce((sum, r) => sum + r.score, 0),
      avgScore: goodDeeds.length > 0 ? (goodDeeds.reduce((sum, r) => sum + r.score, 0) / goodDeeds.length).toFixed(1) : 0,
      categories: {}
    }

    // 恶行统计
    const badDeeds = karmaRecords.value.filter((r) => r.score < 0)
    stats.badDeedsStats = {
      count: badDeeds.length,
      totalScore: badDeeds.reduce((sum, r) => sum + Math.abs(r.score), 0),
      avgScore: badDeeds.length > 0 ? (badDeeds.reduce((sum, r) => sum + Math.abs(r.score), 0) / badDeeds.length).toFixed(1) : 0,
      categories: {}
    }

    // 分类统计
    karmaRecords.value.forEach((record) => {
      const category = record.karmaCategory || '未分类'
      stats.categoryDistribution[category] = (stats.categoryDistribution[category] || 0) + 1
    })

    // 等级分布
    karmaByPerson.value.forEach((person) => {
      const level = getKarmaLevel(person.totalScore).level
      stats.levelDistribution[level] = (stats.levelDistribution[level] || 0) + 1
    })

    return stats
  })

  // 获取业力记录
  const fetchKarmaRecords = async () => {
    loading.value = true
    try {
      // 模拟API调用
      await new Promise((resolve) => setTimeout(resolve, 800))
      karmaRecords.value = [...mockData.value]
    } catch (error) {
      console.error('Failed to fetch karma records:', error)
    } finally {
      loading.value = false
    }
  }

  // 添加业力记录
  const addKarmaRecord = async (recordData) => {
    try {
      const rule = recordData.type === 'goodDeed' ? karmaRules.value.goodDeeds[recordData.action] : karmaRules.value.badDeeds[recordData.action]

      const newRecord = {
        id: Date.now(),
        personId: recordData.personId || Date.now(),
        ...recordData,
        score: rule.score,
        karmaCategory: rule.category,
        date: new Date().toISOString().split('T')[0],
        time: new Date().toTimeString().split(' ')[0].substring(0, 5),
        verified: false,
        createdAt: new Date().toISOString()
      }

      karmaRecords.value.unshift(newRecord)
      return { success: true, record: newRecord }
    } catch (error) {
      return { success: false, error: error.message }
    }
  }

  // 更新业力记录
  const updateKarmaRecord = async (id, recordData) => {
    try {
      const index = karmaRecords.value.findIndex((r) => r.id === id)
      if (index !== -1) {
        karmaRecords.value[index] = { ...karmaRecords.value[index], ...recordData }
        return { success: true, record: karmaRecords.value[index] }
      }
      return { success: false, error: '记录不存在' }
    } catch (error) {
      return { success: false, error: error.message }
    }
  }

  // 删除业力记录
  const deleteKarmaRecord = async (id) => {
    try {
      const index = karmaRecords.value.findIndex((r) => r.id === id)
      if (index !== -1) {
        karmaRecords.value.splice(index, 1)
        return { success: true }
      }
      return { success: false, error: '记录不存在' }
    } catch (error) {
      return { success: false, error: error.message }
    }
  }

  // 计算个人业力等级
  const getKarmaLevel = (totalScore) => {
    const level = karmaLevels.value.find((l) => totalScore >= l.range[0] && totalScore <= l.range[1])
    return level || { level: '未知', color: '#666', description: '业力等级未知' }
  }

  // 预测报应时机
  const predictRetribution = (personId) => {
    const person = karmaByPerson.value.find((p) => p.personId === personId)
    if (!person) return null

    const recentBadDeeds = person.records
      .filter((r) => r.score < 0)
      .filter((r) => {
        const recordDate = new Date(r.date)
        const now = new Date()
        const daysDiff = (now - recordDate) / (1000 * 60 * 60 * 24)
        return daysDiff <= 365 // 最近一年的恶行
      })

    if (recentBadDeeds.length === 0) return null

    const totalBadKarma = Math.abs(recentBadDeeds.reduce((sum, r) => sum + r.score, 0))

    // 按照阈值从高到低的顺序检查，确保更严重的恶行得到更严重的预测
    const orderedRules = [
      { type: 'immediate', ...retributionRules.value.immediate },
      { type: 'shortTerm', ...retributionRules.value.shortTerm },
      { type: 'mediumTerm', ...retributionRules.value.mediumTerm },
      { type: 'longTerm', ...retributionRules.value.longTerm }
    ]

    for (const rule of orderedRules) {
      if (totalBadKarma >= rule.threshold) {
        return {
          type: rule.type,
          probability: rule.probability,
          timeRange: rule.timeRange,
          severity: totalBadKarma,
          recentBadDeeds: recentBadDeeds.length
        }
      }
    }

    return null
  }

  // 业力抵消计算
  const calculateKarmaBalance = (personId) => {
    const person = karmaByPerson.value.find((p) => p.personId === personId)
    if (!person) return null

    const goodKarma = person.records.filter((r) => r.score > 0).reduce((sum, r) => sum + r.score, 0)
    const badKarma = Math.abs(person.records.filter((r) => r.score < 0).reduce((sum, r) => sum + r.score, 0))

    return {
      goodKarma,
      badKarma,
      netKarma: goodKarma - badKarma,
      balanceRatio: badKarma > 0 ? (goodKarma / badKarma).toFixed(2) : 'Infinity',
      canOffset: goodKarma >= badKarma,
      offsetPercentage: badKarma > 0 ? Math.min(100, (goodKarma / badKarma) * 100).toFixed(1) : 100
    }
  }

  return {
    // 状态
    karmaRecords,
    loading,
    karmaRules,
    karmaLevels,
    retributionRules,

    // 计算属性
    totalKarmaCount,
    goodDeedsCount,
    badDeedsCount,
    karmaByPerson,
    karmaStatistics,

    // 方法
    fetchKarmaRecords,
    addKarmaRecord,
    updateKarmaRecord,
    deleteKarmaRecord,
    getKarmaLevel,
    predictRetribution,
    calculateKarmaBalance
  }
})
